<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <span class="header-class">重置密码</span>
      </div>
      <el-form :label-position="labelPosition" label-width="100px" :model="form" :rules="rules" ref="form">
        <el-form-item label="原密码" prop="old_pwd">
          <el-input class="input" v-model="form.old_pwd" placeholder="请输入原密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new_pwd">
          <el-input class="input" v-model="form.new_pwd" placeholder="请输入新密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="re_pwd">
          <el-input class="input" v-model="form.re_pwd" placeholder="请再次输入新密码" show-password></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" style="margin-left: 100px" @click="upPwd">修改密码</el-button>
      <el-button type="primary" plain @click="resetForm('form')">重置</el-button>
    </el-card>
  </div>
</template>

<script>
import { updatePassword } from '@/api/center'
export default {
  // 重置密码
  name: 'ResetPassword',
  data () {
    // 自定义原始密码规则
    const checkPassword = (rule, value, callback) => {
      const regPassword = /^\w{6,15}$/
      if (regPassword.test(value)) {
        return callback()
      }
      callback(new Error('密码格式为6-15位的数字或字母'))
    }
    // 新密码确认
    const validPassword = (rule, value, callback) => {
      if (value === this.form.new_pwd) {
        callback()
      } else {
        callback(new Error('两次密码不一致，请重新输入'))
      }
    }
    return {
      labelPosition: 'right',
      form: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      rules: {
        old_pwd: [{ required: true, message: '请输入原始密码', trigger: 'blur' },
          { pattern: /^\w{6,15}$/, message: '请输入 6 到 15 位的密码', trigger: 'blur' }],
        new_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: checkPassword, trigger: 'blur' }],
        re_pwd: [{ validator: validPassword, trigger: 'blur' },
          { required: true, message: '请输入确认新密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 重置密码
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    // 修改密码
    async upPwd () {
      try {
        // 校验表单
        const valid = await this.$refs.form.validate()
        // 如果校验通过
        if (valid) {
          const { old_pwd, new_pwd, re_pwd } = this.form
          const res = await updatePassword({ old_pwd, new_pwd, re_pwd })
          if (res.code === 1) {
            return this.$message.error('原始密码错误')
          }
          console.log(res)
          this.$message.success('修改密码成功')
          // 删除token
          this.$store.commit('user/logout')
          // 跳到首页
          this.$router.push('/login')
        }
      } catch (e) {
        console.log(e)
      }
    }
  }
}

</script>

<style scoped lang="less">
// 左上重置密码
.header-class {
    line-height: 32px;
    font-size: 16px;
    font-weight: 600;
  }
  // 输入框的宽
 /deep/.input {
  width: 400px;
}
</style>
